<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info storage-product-list">
      <el-form ref="queryForm" :inline="true" :model="queryForm" label-width="90px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="库区" prop="areaName">
              <el-input v-model="queryForm.areaName" placeholder="库区名称/编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联仓库" prop="storageId">
              <el-select v-model="queryForm.storageId">
                <el-option key="0" label="--全部--" :value="0" />
                <el-option v-for="item in storageOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="库区类型" prop="areaType">
              <el-select v-model="queryForm.areaType">
                <el-option key="0" label="--全部--" :value="0" />
                <el-option v-for="item in areaTypeOptions" :key="item.id" :label="item.name"
                  :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联设备ID" prop="relatedDeviceID">
              <el-input v-model="queryForm.relatedDeviceID" placeholder="关联设备ID" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryForm.status">
                <el-option label="全部" :value="0" />
                <el-option label="启用" :value="1" />
                <el-option label="禁用" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="onQuery">查询</el-button>
            <el-button @click="cancelForm">清空</el-button>
          </el-col>
        </el-row>
      </el-form>

      <div class="base-content-operate">
        <el-button v-rights="'Create'" icon="el-icon-plus" type="primary" @click="onNew">新增</el-button>
      </div>

      <el-table :data="list" element-loading-text="Loading" tooltip-effect="dark" style="width: 100%"
        height="calc(100% - 150px)">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="所属仓库">
          <template slot-scope="scope">
            {{ scope.row.storageName }}
          </template>
        </el-table-column>
        <el-table-column label="库区名称">
          <template slot-scope="scope">
            {{ scope.row.areaName }}
          </template>
        </el-table-column>
        <el-table-column label="库区编码">
          <template slot-scope="scope">
            {{ scope.row.areaNumber }}
          </template>
        </el-table-column>
        <el-table-column label="库区类型">
          <template slot-scope="scope">
            {{ scope.row.areaType == 100 ? '普通库区' : '智能设备' }}
          </template>
        </el-table-column>
        <el-table-column label="关联设备ID">
          <template slot-scope="scope">
            {{ scope.row.relatedDeviceID }}
          </template>
        </el-table-column>
        <el-table-column label="作业方式">
          <template slot-scope="scope">
            {{ scope.row.jobWay == 100 ? '人工' : '自动' }}
          </template>
        </el-table-column>
        <el-table-column label="存储条件">
          <template slot-scope="scope">
            {{ getConditionName(scope.row.storageConditions) }}
          </template>
        </el-table-column>
        <el-table-column label="库位数">
          <template slot-scope="scope">
            {{ scope.row.storageSpaceCount }}
          </template>
        </el-table-column>
        <el-table-column label="推荐优先级">
          <template slot-scope="scope">
            {{ scope.row.level }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.status == 1 ? '启用' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作人">
          <template slot-scope="scope">
            {{ scope.row.operatorName }}
          </template>
        </el-table-column>
        <el-table-column label="操作时间">
          <template slot-scope="scope">
            {{ scope.row.updateTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link v-rights="'Update'" size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-link>
              <el-link v-rights="'StorageSpaceEdit'" size="medium" type="primary"
                @click="storageSpaceEdit(scope.row)">库位设置</el-link>
              <el-link v-rights="'Delete'" size="mini" type="danger" style="display: none;"
                @click="handleDelete(scope.row)">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination :total="pagination.total" :current-page="pagination.pageIndex" :page-count="pagination.total"
        @changePage="changePage" @sizeChange="sizeChange" />
    </div>

    <StorageAreaEdit ref="StorageAreaEdit" v-model="dialogFormVisible" :edit-id="editId" @success="DialogCall" />
    <StorageSpaceEdit ref="StorageSpaceEdit" v-model="dialogStorageSpace" :edit-id="editId" @success="DialogCall" />
  </div>
</template>

<script>
import { getDictList } from "../../../api/System/SystemDict.js";
import { deleteRecord, getList, getStorageList } from "../../../api/Storage/StorageArea.js";
import StorageAreaEdit from "./StorageAreaEdit.vue";
import StorageSpaceEdit from "./StorageSpaceEdit.vue";

export default {
  name: "StorageAreaList",
  components: { StorageAreaEdit, StorageSpaceEdit },
  data() {
    return {
      loading: true,
      editId: "",
      list: [],
      storageOptions: [],
      areaTypeOptions: [],
      queryForm: {
        storageId: 0,
        areaName: "",
        areaType: 0,
        relatedDeviceID: "",
        status: 1,
      },
      dialogFormVisible: false,
      dialogStorageSpace: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  created() {
    this.getOptions();
    this.getList();
  },
  methods: {

    getConditionName(id) {
      const conditionMap = {
        100: '常温',
        200: '冷藏',
        300: '冷冻',
      }
      return conditionMap[id] || '';
    },
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },

    getOptions() {
      getDictList({ dictCode: "StorageArea_AreaTypeCode" }).then((res) => {
        this.areaTypeOptions = res.data;
      });

      getStorageList({ type: 0 })
        .then((res) => {
          this.storageOptions = res.data.dataList ? res.data.dataList : [];
        }).catch(() => { });
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.editId = "";
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.editId = rowData.id;
      this.dialogFormVisible = true;
    },
    storageSpaceEdit(rowData) {
      this.editId = rowData.id;
      this.dialogStorageSpace = true;
    },
    onQuery() {
      this.getList();
    },
    // 关闭窗体回调函数
    DialogCall() {
      this.getTree();
    },
  },
};
</script>
<style lang="scss">
.storage-product-list {
  .el-form-item__content {
    width: calc(100% - 90px);
  }
}
</style>
